package com.acwing.partition3;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author `RKC`
 * @date 2021/11/29 11:17
 */
public class AC285没有上司的舞会 {

    private static final int N = 6010, M = N;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[N];
    //f1[i]表示邀请i时，以i为根节点的最大快乐指数；f2[i]表示不邀请i时，以i为根节点的最大快乐指数
    private static int[] f1 = new int[N], f2 = new int[N];
    private static boolean[] st = new boolean[N];
    private static int n, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i++) w[i] = Integer.parseInt(reader.readLine());
        for (int i = 0; i < n - 1; i++) {
            String[] ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            add(b, a);
            st[a] = true;
        }
        int root = -1;
        for (int i = 1; i <= n; i++) {
            if (!st[i]) {
                root = i;
                break;
            }
        }
        dfs(root);
        writer.write(Math.max(f1[root], f2[root]) + "\n");
        writer.flush();
    }

    private static void dfs(int u) {
        f1[u] = w[u];
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            dfs(v);
            f2[u] += Math.max(f1[v], f2[v]);
            f1[u] += f2[v];
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

